package com.fengchao.util.wechat.commonality;

import java.util.HashMap;
import java.util.Map;

import com.fengchao.api.config.WeChatConfig;
import com.fengchao.util.net.HttpUtil;
import com.google.gson.Gson;


/**
 * 微信网页授权工具
 *
 * @author udpate  wang
 */
public class WeChatAuthUtil {

    static Gson gson = new Gson();

    /**
     * 根据code获取accessToken
     *
     * @param code
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> getAccessTokenByCode(String code, String appId, String appSecret) {
        Map<String, String> para = new HashMap<String, String>();
        para.put("appid", appId);
        para.put("secret", appSecret);
        para.put("code", code);
        para.put("grant_type", "authorization_code");
        String jsonResult = HttpUtil.get(WeChatConfig.WECHAT_URL_AUTH_TOKEN, para);
        Map<String, String> map = gson.fromJson(jsonResult, Map.class);
        return map;
        /*成功
         * {
			   "access_token":"ACCESS_TOKEN",
			   "expires_in":7200,
			   "refresh_token":"REFRESH_TOKEN",
			   "openid":"OPENID",
			   "scope":"SCOPE",
			   "unionid": "o6_bmsasdasdsad6_2sgVt7hMZOPfL"
		}
		错误
		{"errcode":40029,"errmsg":"invalid code"}
		*/
    }

    /**
     * 刷新token
     * refresh_token
     *
     * @param
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> refreshTokenByToken(String refreshToken, String appId) {

        Map<String, String> para = new HashMap<String, String>();
        para.put("appid", appId);
        para.put("grant_type", "refresh_token");
        para.put("refresh_token", refreshToken);
        String jsonResult = HttpUtil.get(WeChatConfig.WECHAT_URL_REFRESHTOKEN, para);
        Map<String, Object> access_openId = gson.fromJson(jsonResult, Map.class);
        return access_openId;
        //返回json格式
        /*{
               "access_token":"ACCESS_TOKEN",
			   "expires_in":7200,
			   "refresh_token":"REFRESH_TOKEN",
			   "openid":"OPENID",
			   "scope":"SCOPE"
		}*/
    }

    /**
     * 获取授权之后的用户信息
     *
     * @param accessToken
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> getUserInfobyaccessToken(String accessToken) {
        Map<String, String> para = new HashMap<String, String>();
        para.put("access_token", accessToken);
        para.put("openid", "refresh_token");
        para.put("lang", "zh_CN");
        String jsonResult = HttpUtil.get(WeChatConfig.WECHAT_URL_USERINFO, para);
        Map<String, Object> access_openId = gson.fromJson(jsonResult, Map.class);
        return access_openId;
         /*{
               "openid":" OPENID",
			   " nickname": NICKNAME,
			   "sex":"1",
			   "province":"PROVINCE"
			   "city":"CITY",
			   "country":"COUNTRY",
			    "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", 
				"privilege":[
				"PRIVILEGE1"
				"PRIVILEGE2"
			    ],
			    "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
		}
		*/
    }


}
